//
//  PLRLobbyModel.m
//  PLRLiveShow
//
//  Created by zhaoxiaoyan on 2017/8/15.
//  Copyright © 2017年 xiaoyanzhao. All rights reserved.
//

#import "PLRLobbyModel.h"
#import "PlcRoomInfo.h"

static NSString * const kHost = @"http://livetv.levicode.me/rooms";

@implementation PLRLobbyModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _lobbyArray = [[NSMutableArray alloc] init];
        _imageArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)requestLobbyListWithComplete:(void(^)())complete {
    NSString *url = [NSString stringWithFormat:@"%@", kHost];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"GET";
    request.timeoutInterval = 10;
    
    [request addValue:@"livetv" forHTTPHeaderField:@"author"];
    
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error != nil || response == nil || data == nil) {
            NSLog(@"获取大厅列表失败... %@", error);
        }
        NSDictionary *roomsDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        NSLog(@"1111 %@", roomsDic);
        NSMutableArray *roomsArray = [roomsDic objectForKey:@"data"];
        self.lobbyArray = ({
            NSMutableArray<PlcRoomInfo *> *roomInfos = [[NSMutableArray<PlcRoomInfo *> alloc] init];
            for (NSUInteger i = 0; i < roomsArray.count; i++) {
                NSDictionary *room = roomsArray[i];
                NSMutableDictionary *roomDic = [room objectForKey:@"room"];
                NSMutableDictionary *broadcastDic = [room objectForKey:@"broadcaster"];
                
                PlcRoomInfo *roomInfo = [[PlcRoomInfo alloc] init];
                roomInfo.roomName = roomDic[@"name"];
                roomInfo.playURL = roomDic[@"playUrl"];
                roomInfo.roomID = [roomDic[@"id"] integerValue];
                roomInfo.roomNO = roomDic[@"no"];
                roomInfo.broadcastName = broadcastDic[@"name"];
                roomInfo.imageURL = roomDic[@"snapPic"];
                roomInfo.broadcastID = [broadcastDic[@"id"] integerValue];
                roomInfo.broadcastSex = [broadcastDic[@"sex"] integerValue];
                [roomInfos addObject:roomInfo];
            }
            roomInfos;
        });
        
        for (int i = 0; i < _lobbyArray.count; i++) {
            dispatch_sync(dispatch_get_global_queue(0, 0), ^{
                if (self.lobbyArray != nil && self.lobbyArray.count>0) {
                    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_lobbyArray[i].imageURL]];
                    UIImage *image = [UIImage imageWithData:data];
                    [_imageArray addObject:image];
                }
            });
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (complete) {
                complete();
            }
        });
    }];
    [task resume];
}

@end
